package com.example.boxugu.myapplication.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boxugu.myapplication.Moder.ChangePasswdModer;
import com.example.boxugu.myapplication.Moder.UserInfoModer;
import com.example.boxugu.myapplication.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class ChangePasswdActivity extends AppCompatActivity {
     ImageButton imageButton;
     Button mtn;
     String token;
     EditText moldpasswd,mnewpasswd,mnewpasswd2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_passwd);
        imageButton=findViewById(R.id.imgbtn_back);
        mtn=findViewById(R.id.save_btn);
        moldpasswd=findViewById(R.id.edit_oldpassword);
        mnewpasswd=findViewById(R.id.edit_newpassword);
        mnewpasswd2=findViewById(R.id.edit_newpassword2);
        mtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SharedPreferences sharedPreferences=getSharedPreferences("boxugu", Context.MODE_PRIVATE);
                token=sharedPreferences.getString("token","");
                if (token.length()>0){
                    RequestParams params = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/changepassword");
                    params.addBodyParameter("token",token);
                    String olderpasswd=moldpasswd.getText().toString();
                    String newpasswold=mnewpasswd.getText().toString();
                    String newpassword2=mnewpasswd2.getText().toString();
                    if (TextUtils.isEmpty(olderpasswd)){
                        Toast.makeText(ChangePasswdActivity.this,"请输入旧密码",Toast.LENGTH_LONG).show();
                        return;
                    }if(TextUtils.isEmpty(newpasswold)){
                        Toast.makeText(ChangePasswdActivity.this,"请输入新密码",Toast.LENGTH_LONG).show();
                        return;
                    }if(TextUtils.isEmpty(newpassword2)){
                        Toast.makeText(ChangePasswdActivity.this,"请再次输入新密码",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (!newpasswold.equals(newpassword2)){
                        Toast.makeText(ChangePasswdActivity.this,"两次输入的新密码不对",Toast.LENGTH_LONG).show();
                        return;
                    }
                    params.addBodyParameter("olderpasswd",olderpasswd);
                    params.addBodyParameter("newpasswold",newpasswold);

                    x.http().post(params, new Callback.CacheCallback<String>() {
                        @SuppressLint("ResourceType")
                        @Override
                        public void onSuccess(String result) {
                            Gson gson = new Gson();
                            ChangePasswdModer changePasswdModer = gson.fromJson(result, ChangePasswdModer.class);
                            Log.i("changePasswdModer","onSuccess");
                            if (changePasswdModer!=null&&changePasswdModer.msg!=null){
                                if (changePasswdModer.code==0){
                                    Toast.makeText(ChangePasswdActivity.this,"保存成功",Toast.LENGTH_LONG).show();
                                    SharedPreferences sp = getSharedPreferences("boxugu", MODE_PRIVATE);
                                    SharedPreferences.Editor editor=sp.edit();
                                    editor.commit();
                                    Intent intent=new Intent(ChangePasswdActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();

                                }else {
                                    Toast.makeText(ChangePasswdActivity.this,"保存失败",Toast.LENGTH_LONG).show();
                                }

                            }


                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {


                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public boolean onCache(String result) {
                            return false;
                        }
                    });
                }
                finish();
            }
        });
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
